<?php

namespace Client\Controller;

use Think\Controller;
use Org\Net\Http;

class ScenicDiscussController extends Controller {
	/**
	 * 添加评论
	 * sid 景区ID
	 * aid 用户ID
	 * content 评论内容
	 */
	public function addDiscuss() {
    	if(IS_POST){
			$data = I ( 'post.' );
			$discuss = D ( "ScenicDiscuss" );
			$data = $discuss->create ( $data );
			if ($data) {
				$result = $discuss->add ( $data );
				if ($result) {
					// 清空评论缓存
					S ( "ScenicDiscuss_{$data['id']}", NULL );
					$json ['result'] = 0;
					$json ['msg'] = "评论成功";
				} else {
					$json ['result'] = - 2;
					$json ['msg'] = "评论失败";
				}
			} else {
				$json ['result'] = - 1;
				$json ['msg'] = $discuss->getError ();
			}
			$this->ajaxReturn ( $json );
		}
	}
	/**
	 * 刷新评论
	 */
	public function refreshDiscuss(){
		if(IS_POST){
			$id = I ( "sid" );
			S ( "ScenicDiscuss_{$id}",null);
			$this->getDiscuss();
		}
	}
	/**
	 * 获取评论
	 */
	public function getDiscuss() {
		if (IS_POST) {
			$id = I ( "sid" );
			$list = S ( "ScenicDiscuss_{$id}" );
			$pageSize = 5;
	    	$page = I('page');
	    	if(isset($page)){
	    		$start = $page*$pageSize;
	    	}
	    	if(!$list){
	    		$discuss = D ( "ScenicDiscussView" );
		   		$list = $discuss->cache("ScenicDiscuss_{$id}")->where(array('sid'=>$id))->order('ctime desc')->select();
	    	}
		   	$res = array_slice($list,$start,$pageSize);
		   	if(count($res)>0){
				$json ['result'] = 0;
				$json ['msg'] = "成功";
				$json ['discuss'] = $res;
			} else {
				$json ['result'] = - 1;
				$json ['msg'] = "暂无评论";
			}
			$this->ajaxReturn ( $json );
		}
	}
}